package dao;


import com.google.gson.Gson;
import pojo.Tip;
import utils.JDBCUtils;


import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class Tipdao {

    public List<Tip> list(String sql,String uid) throws SQLException {
        // 1获得DB的连接
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement pst = connection.prepareCall(sql);
        pst.setString(1,uid);
        ResultSet rs= pst.executeQuery();
        List<Tip> resultList = new ArrayList<>();
        while(rs.next()){
            Tip t = ResultSetToBean(rs);
            resultList.add(t);
        }
        return resultList;
    }

    public List<Tip> list(String sql) throws SQLException {
        // 1获得DB的连接
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement pst = connection.prepareCall(sql);
        ResultSet rs= pst.executeQuery();
        List<Tip> resultList = new ArrayList<>();
        while(rs.next()){
            Tip t = ResultSetToBean(rs);
            resultList.add(t);
        }
        return resultList;
    }



    public String addtip(String sql, String uid, String seat,String money, String details) throws SQLException {
        Connection connection = JDBCUtils.getConnection();
        System.out.println(seat);
        PreparedStatement pst = connection.prepareCall(sql);
        pst.setString(1,uid);
        pst.setString(2,seat);
        pst.setString(3,money);
        pst.setString(4,details);
        int ret_row = pst.executeUpdate();

        HashMap<String,Object> resultMap = new HashMap<>();
        if (ret_row>0)resultMap.put("state","add success");
        else resultMap.put("state","add faild");
        return new Gson().toJson(resultMap);
    }

    public String updattip(String sql, String orderNum, String status) throws SQLException {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement pst = null;
        pst = connection.prepareCall(sql);
        pst.setString(1,status);
        pst.setString(2,orderNum);
        int ret_row = pst.executeUpdate();
        HashMap<String,Object> resultMap = new HashMap<>();
        if (ret_row>0)resultMap.put("state","update success");
        else resultMap.put("state","update faild");
        return new Gson().toJson(resultMap);
    }

    private Tip ResultSetToBean(ResultSet rs) throws SQLException {
        Tip tip = null;
        String userId = rs.getString("userId");
        String orderNum = rs.getString("orderNum");
        String seat = rs.getString("seat");
        String orderTime = rs.getString("orderTime");
        String status = rs.getString("status");
        String details = rs.getString("details");
        String money = rs.getString("money");
        tip = new Tip(userId,orderNum,seat,orderTime,money,status,details);
        return tip;
    }


    public String deletetip(String sql, String orderNum) throws SQLException {
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement pst = null;
        pst = connection.prepareCall(sql);
        pst.setString(1,orderNum);
        int ret_row = pst.executeUpdate();
        HashMap<String,Object> resultMap = new HashMap<>();
        if (ret_row>0)resultMap.put("state","delete success");
        else resultMap.put("state","delete faild");
        return new Gson().toJson(resultMap);
    }
}
